<script setup lang="ts">
import Taro, { hideLoading, showLoading } from '@tarojs/taro';
import { storeToRefs } from 'pinia';
import { useNurseUser } from '@/service';
import { useNursesStore, useAuthStore } from '@/store';
import { useGetAuthInfo, useAuthLogin, useNurseAuthLogin, useNurseGetAuthInfo, useGetOpenid } from '@/hooks';
import { localStg } from '@/utils';
import UserHome from './components/userhome.vue';
// import NursesHome from './components/nurseshome.vue';
import { getDemographicInfo } from "./tooler";
import { getDictWithCache } from "@/service/api/dict";
import { request } from '@/service/request';

definePageConfig({
  navigationStyle: 'custom',
  navigationBarTitleText: '安好护理',
  enablePullDownRefresh: true
});

const { nurseNurses, openid: nurse_openid } = storeToRefs(useNursesStore());

const { openid, nurseUser } = storeToRefs(useAuthStore());
// import { useGetPhone, useGetOpenid } from '@/hooks';

const { refreshSpecial, getVisibleByHospital } = useNurseUser();

Taro.usePullDownRefresh(async () => {
  try {
    showLoading({ title: '刷新中...' });
    Taro.reLaunch({
      url: '/pages/index/index'
    })
  } finally {
    hideLoading();
    Taro.stopPullDownRefresh();
  }
})

async function initAuth() {
  const openid_Stg = localStg.get('openid');

  if (openid_Stg) {
    showLoading({ title: '正在请求...' });
    openid.value = openid_Stg;
    await useAuthLogin();
    await useGetAuthInfo();
    if (nurseUser.value.id) refreshSpecial(nurseUser.value.id);
    hideLoading();
    const res = await getDemographicInfo(nurseUser.value.idcard);
    // res
    if(res){
      localStg.set("demographicInfo", res);
       const obj = localStg.get("demographicInfo");
      // obj

      const rs = await getDictWithCache("job_type");
      // code

      const { error, success } = await request.get<any>(`/sys/dict/list`, {
        pageSize: 1000
      }, {
          useErrMsg: true
      });
      // code success
    }
  }
  else{
    // useGetOpenid()
  }

  const nurseopenid = localStg.get('nurse_openid');
  if (nurseopenid) {
    showLoading({ title: '正在请求...' });
    nurse_openid.value = nurseopenid;
    await useNurseAuthLogin();
    await useNurseGetAuthInfo();
    hideLoading();
  }

  // Taro.login({
  //   success: (res) => {
  //     // res
  //     useGetPhone(res.code);
  //   }
  // });
  // 用户未登录
}

initAuth();
</script>
<template>
  <basic-layout show-tab-bar>
    <!-- <NursesHome v-if="nurse_openid && nurseNurses?.id" /> -->
    <!-- <UserHome v-if="!nurse_openid && !nurseNurses?.id" /> -->
    <UserHome v-if="!nurse_openid && !nurseNurses?.id" />
  </basic-layout>
</template>
